//
//  ZSBaseNavigationController.swift
//  creativeassistant
//
//  Created by 灵工邦 on 2025/1/22.
//

import UIKit

class ZSBaseNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        UINavigationBar.appearance().isTranslucent = false
        UINavigationBar.appearance().tintColor = .black
        UINavigationBar.appearance().barTintColor = .white
        let textAttributes = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),
            NSAttributedString.Key.foregroundColor: UIColor.black
        ]
        if #available(iOS 13, *) {
            let appearance: UINavigationBarAppearance = UINavigationBarAppearance()
            appearance.titleTextAttributes = textAttributes
            appearance.backgroundColor = .white
            appearance.shadowColor = .white
            UINavigationBar.appearance().standardAppearance = appearance
        }else {
            UINavigationBar.appearance().titleTextAttributes = textAttributes
            UINavigationBar.appearance().setBackgroundImage(UIImage.init(), for: .default)
            UINavigationBar.appearance().shadowImage = UIImage()
        }
        if #available(iOS 11, *) {
            UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -200, vertical: 0), for: .default)
        }else {
            UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: 60), for: .default)
        }
        weak var weakSelf = self
        self.delegate = weakSelf
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        if ((self.navigationController?.responds(to: #selector(getter: interactivePopGestureRecognizer))) != nil) {
            self.interactivePopGestureRecognizer?.isEnabled = false
        }
        super.pushViewController(viewController, animated: animated)
    }
}

extension ZSBaseNavigationController: UINavigationControllerDelegate{
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if ((self.navigationController?.responds(to: #selector(getter: interactivePopGestureRecognizer))) != nil) {
            self.interactivePopGestureRecognizer?.delegate = self
            self.interactivePopGestureRecognizer?.isEnabled = true
        }
        if navigationController.viewControllers.count == 1 {
            navigationController.interactivePopGestureRecognizer?.isEnabled = false
            navigationController.interactivePopGestureRecognizer?.delegate = nil
        }
    }
    
}

extension ZSBaseNavigationController: UIGestureRecognizerDelegate{

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return gestureRecognizer.isKind(of: UIScreenEdgePanGestureRecognizer.self)
    }
}

extension ZSBaseNavigationController {
    
    override var shouldAutorotate: Bool {
        guard let lastViewController = self.viewControllers.last else {
            return super.shouldAutorotate
        }
        return lastViewController.shouldAutorotate
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        guard let lastViewController = self.viewControllers.last else {
            return super.preferredInterfaceOrientationForPresentation
        }
        return lastViewController.preferredInterfaceOrientationForPresentation
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        guard let lastViewController = self.viewControllers.last else {
            return super.supportedInterfaceOrientations
        }
        return lastViewController.supportedInterfaceOrientations
    }
    
}

